﻿#include <iostream>
#include <unordered_map>
#include <vector>

class Bridge {
private:
    std::unordered_map<int, int> macTable;  // MAC地址表：地址 -> 端口
    const int port1 = 1;
    const int port2 = 2;

public:
    void processFrame(int srcAddr, int srcPort, int destAddr) {
        // 自学习：记录源地址和端口
        macTable[srcAddr] = srcPort;

        // 确定目的端口
        std::vector<int> outputPorts;
        if (destAddr == 0xF) {  // 广播地址
            outputPorts = { port1, port2 };
        }
        else if (macTable.find(destAddr) != macTable.end()) {
            int destPort = macTable[destAddr];
            if (destPort != srcPort) {  // 避免回传
                outputPorts.push_back(destPort);
            }
        }
        else {  // 未知地址，泛洪到另一个端口
            outputPorts.push_back(srcPort == port1 ? port2 : port1);
        }

        // 输出结果
        std::cout << "目标端口: ";
        for (int port : outputPorts) {
            std::cout << port << " ";
        }
        std::cout << "\nMAC地址表:\n";
        printMacTable();
    }

    void printMacTable() {
        for (auto& entry : macTable) {
            std::cout << "地址: 0x" << std::hex << entry.first
                << " -> 端口: " << std::dec << entry.second << "\n";
        }
        std::cout << "-----------------\n";
    }
};

int main() {
    Bridge bridge;
    int srcAddr, srcPort, destAddr;

    std::cout << "输入格式（十六进制）: 源地址 源端口 目的地址\n";
    std::cout << "示例：0xA 1 0xB\n输入结束请输入 EOF\n";

    while (std::cin >> std::hex >> srcAddr >> srcPort >> std::hex >> destAddr) {
        if (srcPort < 1 || srcPort > 2) {
            std::cerr << "错误：源端口只能是1或2\n";
            continue;
        }
        std::cout << "\n处理帧: 源=0x" << std::hex << srcAddr
            << " 端口=" << std::dec << srcPort
            << " 目的=0x" << std::hex << destAddr << "\n";
        bridge.processFrame(srcAddr, srcPort, destAddr);
    }

    return 0;
}